package com.su.czl.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.su.czl.config.CommonResult;
import com.su.czl.entity.Dictionarys;
import com.su.czl.service.DictService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictHandler {
    @Resource
    private DictService dictService;
    
    //查询所有语句
    @RequestMapping("selects")
    public PageInfo<Dictionarys> orgInfs(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 8);
        List<Dictionarys> selects = dictService.selects();
        PageInfo<Dictionarys> pageInfo = new PageInfo<>(selects);
        return pageInfo;
    }
    
    //点击查询方法
    @RequestMapping("/checks")
    public PageInfo<Dictionarys> selectDictsInRange(@RequestParam("dict_name") String dict_name){
        PageHelper.startPage(1, 8);
        List<Dictionarys> dictionarys = dictService.selectDictsInRange(dict_name);
        PageInfo<Dictionarys> pageInfo = new PageInfo<>(dictionarys);
        System.out.println("查询的数据为："+ pageInfo);
        return pageInfo;
    }
    
    //添加数据
    @PostMapping("/add")
    public CommonResult<Dictionarys> addDict(@RequestParam("dict_name") String dict_name,
                                             @RequestParam("dict_type") String dict_type,
                                             @RequestParam("remark") String remark){
        Integer i = dictService.addDict(dict_name, dict_type, remark);
        if (i==1){
            return new CommonResult<>(200,"添加成功!");
        }else {
            return new CommonResult<>(500,"添加失败!");
        }
    }
    
    @RequestMapping("/updata")
    public CommonResult<Dictionarys> updateDict(@RequestParam("id") Integer id,
                                        @RequestParam("dict_name") String dict_name,
                                        @RequestParam("dict_type") String dict_type,
                                       @RequestParam("remark") String remark){
        Integer i = dictService.updataDict(id, dict_name, dict_type, remark);
        if(i==-1){
            return new CommonResult<>(250,"更新失败，没有数据!");
        }else if (i == 1){
            return new CommonResult<>(200,"更新成功!");
        }else {
            return new CommonResult<>(500,"未知原因，更新失败!");
        }
    }
    
    @PostMapping("/delete")
    public Integer deleteById(@RequestParam("id") Integer id){
        Integer i = dictService.deleteById(id);
        return i;
    }

    @PostMapping("/deletes")
    public CommonResult<Dictionarys> deleteDictionaries(@RequestParam String ids) {
        int a=0;
        String[] split = ids.split(",");
        int[] idsArray = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            idsArray[i] = Integer.parseInt(split[i].trim()); // 去除可能的前后空格并转换类型
        }
        for (int j = 0; j < idsArray.length; j++){
            Integer i = dictService.deleteById(idsArray[j]);
            if (i == 1){
                a++;
            }
        }
        if (a==idsArray.length){
            return new CommonResult<>(200,"删除成功！");
        }else if(a < idsArray.length){
            return new CommonResult<>(300,"有一部分删除成功，也有一部分已经删除");
        }else {
            return new CommonResult<>(500,"未知错误，删除失败");
        }
    }

    @RequestMapping("/export")
    public void exportDicts(HttpServletResponse response) {
        try {
            List<Dictionarys> dictionarys = dictService.selects(); // 假设您想导出所有字典数据
            dictService.exportDictsToExcel(dictionarys, response);
        } catch (IOException e) {
            // 处理异常，例如返回错误信息
            e.printStackTrace();
        }
    }
    
}
